﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;
using System.Collections.Specialized;
using Parichay.Domain.Interface.Data;
//using Parichay.Domain.Interface.Service;
using Parichay.Data.Repository;

namespace Parichay.Common.Util
{
    // StructureMap ContollerFactory
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController
            GetControllerInstance(RequestContext requestContext,
            Type controllerType)
        {
            try
            {
                if ((requestContext == null) || (controllerType == null))
                    return null;

                return (Controller)ObjectFactory.GetInstance(controllerType);
            }
            catch (StructureMapException)
            {
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw new Exception(ObjectFactory.WhatDoIHave());
            }
        }
    }

    public static class Bootstrapper
    {
        public static void Run()
        {
            ControllerBuilder.Current
                .SetControllerFactory(new StructureMapControllerFactory());

            //ObjectFactory.Initialize(x =>
            //{
            //    x.AddConfigurationFromXmlFile("StructureMap.xml");
            //});
            ObjectFactory.Initialize(x =>
            {
                x.For<IMessageRepository>().Use<MessageRepository>();
                x.For<IMemberRepository>().Use<MemberRepository>();
                x.For<IMemberPostsRepository>().Use<MemberPostsRepository>();
                x.For<ILogRepository>().Use<LogRepository>();
                x.For<IMemberAlertRepository>().Use<MemberAlertRepository>();
                x.For<IMemberFriendsRepository>().Use<MemberFriendsRepository>();
                x.For<IMemberUploadsRepository>().Use<MemberUploadsRepository>();
                x.For<IMemberConnectsRepository>().Use<MemberConnectsRepository>(); 
                x.For<IMemberGroupsRepository>().Use<MemberGroupsRepository>();
            });
        }

        public static IContainer Initialize()
        {
            ObjectFactory.Initialize(x =>
            {
                x.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });
                //                x.For<IExample>().Use<Example>();
                x.For<IMessageRepository>().Use<MessageRepository>();
                x.For<IMemberRepository>().Use<MemberRepository>();
                x.For<IMemberPostsRepository>().Use<MemberPostsRepository>();
                x.For<ILogRepository>().Use<LogRepository>();
                x.For<IMemberAlertRepository>().Use<MemberAlertRepository>();
                x.For<IMemberFriendsRepository>().Use<MemberFriendsRepository>();
                x.For<IMemberUploadsRepository>().Use<MemberUploadsRepository>();
                x.For<IMemberConnectsRepository>().Use<MemberConnectsRepository>();
                x.For<IMemberGroupsRepository>().Use<MemberGroupsRepository>();
            });
            return ObjectFactory.Container;
        }
    }
}
